package com.ziyun.erp.web.modules.goodsCategory.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.netflix.discovery.converters.Auto;
import com.sun.org.apache.xpath.internal.operations.Bool;
import com.ziyun.erp.common.utils.GoodsMonitorUtils;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.goods.entity.GoodsTemplateEntity;
import com.ziyun.erp.modules.goods.service.GoodsTemplateService;
import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryVO;
import com.ziyun.erp.modules.goodsMonitor.entity.GoodsMonitorEntity;
import com.ziyun.erp.modules.goodsMonitor.service.GoodsMonitorService;
import io.renren.common.utils.StringUtil;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ziyun.erp.modules.goodsCategory.entity.GoodsCategoryEntity;
import com.ziyun.erp.modules.goodsCategory.service.GoodsCategoryService;
import io.renren.common.utils.R;



/**
 * 商品分类表
 *
 * @author
 * @email
 * @date 2018-08-17 13:22:48
 */
@RestController
@RequestMapping("goodsCategory/goodscategory")
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @Autowired
    private GoodsTemplateService goodsTemplateService;

    @Autowired
    private GoodsMonitorService goodsMonitorService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goodsCategory:goodscategory:list")
    public List<GoodsCategoryEntity> list(@RequestParam Map<String, Object> params){
        List<GoodsCategoryEntity> goodsCategoryEntityList = goodsCategoryService.selectList(new EntityWrapper<GoodsCategoryEntity>()
                .eq("disabled", 0)
        );
//        for(GoodsCategoryEntity goodsCategoryEntity : goodsCategoryEntityList){
//            GoodsCategoryEntity parentGoodsCategoryEntity = goodsCategoryService.selectById(goodsCategoryEntity.getParentCategoryId());
//            if(parentGoodsCategoryEntity != null){
//                goodsCategoryEntity.setParentName(parentGoodsCategoryEntity.getCategoryName());
//            }
//        }

        return goodsCategoryEntityList;
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{categoryId}")
//    @RequiresPermissions("goodsCategory:goodscategory:info")
    public R info(@PathVariable("categoryId") String categoryId){
        Integer catId = StringUtil.getRequestParamInteger(categoryId);
        return goodsCategoryService.info(catId);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goodsCategory:goodscategory:save")
    public R save(@RequestBody GoodsCategoryEntity goodsCategory){
        goodsCategory.setCreateName(UserUtils.getCurentUser().getUsername());
        goodsCategoryService.insertGoodsCategoryAndCategoryRelation(goodsCategory);
        return R.ok();
    }

    /**
     * 修改-->废弃
     */
    @RequestMapping("/update")
    @RequiresPermissions("goodsCategory:goodscategory:update")
    public R update(@RequestBody GoodsCategoryEntity goodsCategory){
        ValidatorUtils.validateEntity(goodsCategory);
//        goodsCategoryService.updateAllColumnById(goodsCategory);//全部更新
        GoodsCategoryEntity oldGoodsCategory = goodsCategoryService.selectAllGoodsCategory(goodsCategory.getCategoryId());
        GoodsMonitorEntity goodsMonitorEntity = GoodsMonitorUtils.packingData(oldGoodsCategory, goodsCategory, UserUtils.getCurentUser().getUserId());
        if(goodsMonitorEntity.getSdbId()!= null){
            goodsMonitorService.insert(goodsMonitorEntity);
        }
        goodsCategoryService.updateGodosCategory(goodsCategory);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goodsCategory:goodscategory:delete")
    public R delete(@RequestBody String categoryId){
//      goodsCategoryService.deleteBatchIds(Arrays.asList(categoryIds));
//        Integer requestParamInteger = StringUtil.getRequestParamInteger(categoryId);
        Integer requestParamInteger = Integer.parseInt(StringUtil.toStr(categoryId).trim());
        goodsCategoryService.flagDel(requestParamInteger);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/hide/{hide}")
    @RequiresPermissions("goodsCategory:goodscategory:hide")
    public R hide(@RequestBody String categoryId,@PathVariable("hide") Boolean hide){
//      goodsCategoryService.deleteBatchIds(Arrays.asList(categoryIds));
//        Integer requestParamInteger = StringUtil.getRequestParamInteger(categoryId);
        Integer requestParamInteger = Integer.parseInt(StringUtil.toStr(categoryId).trim());
        goodsCategoryService.hide(requestParamInteger,hide);
        goodsCategoryService.handHide(requestParamInteger,hide);

        return R.ok();
    }
    /**
     * 选择菜单(添加、修改菜单)
     */
    @RequestMapping("/select")
    public R select() {
        List<GoodsCategoryEntity> goodsCategoryEntityList = goodsCategoryService.select();
        return R.ok().put("goodsCategoryEntityList",goodsCategoryEntityList);
    }

    /**
     * 按categoryId获得其子分类集合
     */
    @RequestMapping("/getSonCategory/{categoryId}")
    public R getSonCategory(@PathVariable("categoryId") Integer categoryId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("disabled",0);
        map.put("parent_category_id",categoryId);
        List<GoodsCategoryEntity> goodsCategoryList = goodsCategoryService.selectByMap(map);

        return R.ok().put("goodsCategoryList", goodsCategoryList);
    }

    /**
     * 按categoryId获得三级分类全称和商品模板对象
     */
    @RequestMapping("/getCatNames/{categoryId}")
    public R getCatNames(@PathVariable("categoryId") Integer categoryId){
//        List<GoodsCategoryEntity> goodsCategoryList = goodsCategoryService.getCatNamesByCategoryId(categoryId);
//        StringBuffer categoryNames = new StringBuffer();
//        for (GoodsCategoryEntity goodsCategoryEntity:goodsCategoryList) {
//            categoryNames.append(goodsCategoryEntity.getCategoryName());
//            categoryNames.append("->");
//        }
//        GoodsCategoryEntity categoryEntity = goodsCategoryService.selectById(categoryId);
//        categoryNames.append(categoryEntity.getCategoryName());
//        //根据分类id获得商品模板对象
//        GoodsTemplateEntity goodTemplate = goodsTemplateService.getTemplateByCatId(categoryId);
//        //判断模板对象是否含有工艺选项
//        String attributeTypes = goodsTemplateService.getAttributeTypes(goodTemplate.getTemplateId());
//        if(attributeTypes.contains("craft")){
//            goodTemplate.setIncludCraft(true);
//        }else{
//            goodTemplate.setIncludCraft(false);
//        }
//        return R.ok().put("categoryNames", categoryNames.toString()).put("goodTemplate",goodTemplate).put("categoryEntity",categoryEntity);
        return goodsCategoryService.getCatNamesByCategoryId(categoryId);
    }

    /**
     * 接收表单数据,更新goods_category和goods_category_relation
     */
    @PostMapping("updateCategory")
    public R updateCategory(GoodsCategoryVO goodsCategoryVO) {
        goodsCategoryService.updateGoodsCategoryAndCategoryRelationById(goodsCategoryVO);
        return R.ok();
    }

    /**
     * 获取商品类型枚举
     */
    @GetMapping("/getGoodsType")
    public R getGoodsType() {
        return goodsCategoryService.getGoodsType();
    }

    /**
     * 获取商品分类树
     * @return
     */
    @RequestMapping("/getCategoryTree")
//    @RequiresPermissions("quoteProduct:quoteproductbaseinfo:info")
    public R getAddInfo(){
        Map addInfoMap = goodsCategoryService.getAddInfo();
        return R.ok().put("addInfo",addInfoMap);
    }


}
